<?php

include 'common.php';
include 'gpsfix.php';
class Gps{

    public $gpsinfo=['lat'=>0,'lon'=>0,'drct'=>'','speed'=>0];
	public $nowdate=0;
    public $drct=-1;

    function parseNmea($nmea){

        if (!$this->checkNmea($nmea)){
			return $this->gpsinfo;
        }

        $nmea = explode(',',$nmea);
//        print_r($nmea);
        if($nmea[0]=='$GNGGA'){
            $this->parseGGA($nmea);
        }

        if($nmea[0]=='$GNVTG'){
            $this->parseVTG($nmea);
        }

        if($nmea[0]=='$GNRMC'){
            $this->parseRMC($nmea);
        }

        return $this->gpsinfo;
    }

    function checkNmea($nmea){
		if(strlen($nmea)<5) return false;
        $s=ord($nmea[1]);

        for($i=2;$i<strlen($nmea)-3;$i++){
            $s =  $s ^ ord(substr($nmea,$i,1)) ;
        }

        if(strtoupper(dechex($s)) == substr($nmea,-2)){
            return true;
        }else{
            return false;
        }
    }

    function parseGGA($gga){
		if(!$this->nowdate) $this->nowdate = date('Y-m-d');
        $this->gpsinfo['gps_time'] = $this->nowdate .' '. substr($gga[1],0,2) .':'. substr($gga[1],2,2).':'. substr($gga[1],4,2);
        $this->gpsinfo['gps_time'] = date('Y-m-d H:i:s', strtotime($this->gpsinfo['gps_time'])+28800);
        $lat = $gga[2]/100;
        $lon = $gga[4]/100;
        $this->gpsinfo['lat'] =  (int)$lat + ($lat -(int)$lat)*100/60;
        $this->gpsinfo['lon'] =  (int)$lon + ($lon -(int)$lon)*100/60;
        $this->gpsinfo['dmod'] = $gga[6];
        $this->gpsinfo['hi'] =  $gga[9];
    }

    function parseVTG($vtg){
        $this->gpsinfo['drct'] = $vtg[1];
        $this->gpsinfo['speed'] = (float)$vtg[5] * 1.852;
    }

    function parseRMC($rmc){
    	if(isset($rmc[9])){
    		$this->nowdate = substr($rmc[9],4,2).'-'.substr($rmc[9],2,2).'-'.substr($rmc[9],0,2);
            $this->gpsinfo['gps_time'] = $this->nowdate .' '. substr($rmc[1],0,2) .':'. substr($rmc[1],2,2).':'. substr($rmc[1],4,2);
            $this->gpsinfo['gps_time'] = date('Y-m-d H:i:s', strtotime($this->gpsinfo['gps_time'])+28801);
    	}
        $lat = $rmc[3]/100;
        $lon = $rmc[5]/100;
        $this->gpsinfo['lat'] =  (int)$lat + ($lat -(int)$lat)*100/60;
        $this->gpsinfo['lon'] =  (int)$lon + ($lon -(int)$lon)*100/60;
        $this->gpsinfo['speed'] = (float)$rmc[7] * 1.852;;
        $this->gpsinfo['drct'] = isset($rmc[8])? $rmc[8] :0;
    }
}

#------------------main----------------------

// $gpsfile = "C:/gps_".date('y-m-d_H-i-s').".txt";
$tmpfd=false;
$gpsfd=false;
$gps = new Gps();
$lastTime =0;
$lastData=false;
$lastRktdata='';
$lastPassGps=time();
$startTime=time();

while(1){
	echo 'Loop';
    if(!stripos(php_uname(),'windows')) usleep(10000);

/*
    $s=$redisc->rpop('gpsdata');

    if (!strlen($s)){
		continue;
    };

	$tmpfd = null;
	$s =explode("\r",$s);
	foreach($s as $k=>$v){
		#$s[$k] = trim($v);
		$data = $gps->parseNmea(trim($v));
    }

*/
    $data['sid'] = $Flag['sid'];
    $data['lat'] = 32.02352618;
    $data['lon'] = 109.53824238;
    $data['drct'] = 23;
    $data['hi'] = '238';
    $data['update_time'] = date('Y-m-d H:i:s');

    $Flag = getFlag($db);
    $data['zoneid'] = $Flag['zoneid'];
    $data['shape_type'] = $Flag['shape_type'];
    $data['point_type'] = $Flag['point_type'];

    print_r($data);

    usleep(100000);
    continue;
   
    // print_r($data);
    echo "\r\n========startSecond:".(time()-$startTime)." GPS OK ===========\r\n";
  
    $temperature =  $redisc->get('temperature');
    $data['temperature'] = $temperature ? $temperature :0;

    $lastData=$data;
    $db->update('carlist',$data,'sid='.$conf['sid']);
    $db->insert('gpsinfo',$data);

  //  $redisc->rpush('yzgs_gps',json_encode($data));

}



